home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 January: Mac OS SDK / Dev.CD Jan 00 SDK1.toast / Development Kits / Mac OS / Multiprocessing 2.0 SDK / Sample Code / SortPictsMP ƒ / Sprocket / Lib / Window.cp < prev   
Encoding:
Text File  |  1999-08-03  |  23.9 KB  |  946 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        Window.cp
  3.  
  4.     Contains:    Implementation of TWindow, a base class which provides a
  5.                 framework for building way-cool windows which even John
  6.                 Sullivan would be happy with. Floating windows and “smart
  7.                 zooming” algorithms are based on code samples provided by
  8.                 Dean Yu. Tim Craycroft, the guy making the window manager
  9.                 do all this work for you has also been a great help.
  10.                 
  11.     Written by: Dave Falkenburg
  12.  
  13.     Copyright:    © 1993-94 by Dave Falkenburg, all rights reserved.
  14.  
  15.     Change History (most recent first):
  16.     
  17.          <8>    11/17/94    DRF        Add casts for CFront & PPCC. Also dealt with the change to
  18.                                     ClipAbove in the latest universal headers.
  19.          <7>    11/12/94    DRF        Added AdjustMenusBeforeMenuSelection.
  20.          <6>     11/8/94    DRF        Add some better menu handling methods.
  21.          <5>     9/27/94    DRF         AppLib.h is now Sprocket.h
  22.          <4>      9/9/94    DRF        Reorganized headers and removed redundant #includes.
  23.          <3>      9/4/94    DRF        Added DrawJustTheGrowIcon.
  24.          <2>     8/27/94    DRF        In TWindow::Close, call window’s (de)Activate method before
  25.                                     closing so that menus can be properly updated.
  26.     
  27.     To Do:        Make sure invisible windows can be created & managed
  28.                 Handle modal windows as another class of windows
  29.                 Fix activate bugs when showing and hiding windows
  30.                 Window positioning methods (getters and setters)
  31.                 Display Manager support
  32.                 Changes to support AEObject model
  33.  */
  34.  
  35. #include "Sprocket.h"
  36. #include "Window.h"
  37.  
  38. #include <Script.h>        //    for GetMBarHeight()
  39. #include <LowMem.h>        //    for LMGetWindowList()
  40.  
  41. const short            kFloatingWindowKind        = 1000;
  42. const short            kNormalWindowKind        = 1001;
  43. const WindowPtr     kNoFloatingWindows        = (WindowPtr) -1;
  44.  
  45. const short            kScreenEdgeSlop            = 4;
  46. const short            kSpaceForFinderIcons    = 64;
  47. const short            kMinimumTitleBarHeight    = 21;
  48. const short            kMinimumWindowSize        = 32;
  49.  
  50. static void            HiliteShowHideFloatingWindows(Boolean hiliting,Boolean hiding);
  51.  
  52. static void            FindScreenRectWithLargestPartOfWindow(WindowPtr aWindow,Rect *theBestScreenRect, GDHandle * theBestDevice);
  53. static pascal void    CalculateWindowAreaOnDevice(short depth,short deviceFlags,GDHandle targetDevice,long userData);
  54.  
  55. struct    CalcWindowAreaDeviceLoopUserData
  56.     {
  57.     GDHandle    fScreenWithLargestPartOfWindow;
  58.     long        fLargestArea;
  59.     Rect        fWindowBounds;
  60.     };
  61.  
  62. TWindow::TWindow()
  63.     {
  64.     }
  65.  
  66. TWindow::~TWindow()
  67.     {
  68.     }
  69.  
  70. void
  71. TWindow::CreateWindow(WindowType typeOfWindowToCreate /* = kNormalWindow */)
  72.     {
  73.     WindowPtr    behindWindow,oldFrontMostWindow;
  74.     
  75.     if (typeOfWindowToCreate == kModalWindow)
  76.         {
  77.         DebugStr((StringPtr) "\pModal windows aren’t supported yet");
  78.         fWindowType = kFloatingWindow;
  79.         return;
  80.         }
  81.     else if (typeOfWindowToCreate == kFloatingWindow)
  82.         {
  83.         behindWindow = (WindowPtr) -1;
  84.         oldFrontMostWindow = FrontWindow();
  85.  
  86.         fWindowType = kFloatingWindow;
  87.         }
  88.     else if (typeOfWindowToCreate == kNormalWindow)
  89.         {
  90.         behindWindow = LastFloatingWindow();
  91.  
  92.         fWindowType = kNormalWindow;
  93.         
  94.         if (behindWindow == kNoFloatingWindows)
  95.             oldFrontMostWindow = nil;
  96.         else
  97.             oldFrontMostWindow = (WindowPtr) ((WindowPeek) behindWindow)->nextWindow;
  98.         }
  99.  
  100.     fWindow = this->MakeNewWindow(behindWindow);
  101.     fIsVisible = ((WindowPeek) fWindow)->visible;
  102.  
  103.     if (fWindow)
  104.         {
  105.         SetWRefCon(fWindow,(long) this);
  106.  
  107.         if (typeOfWindowToCreate == kModalWindow)
  108.             {
  109.             DebugStr((StringPtr) "\pCan’t create Modal windows yet");
  110.             }
  111.         else if (typeOfWindowToCreate == kFloatingWindow)
  112.             {
  113.             ((WindowPeek) fWindow)->windowKind = kFloatingWindowKind;
  114.             
  115.             //    make sure the other window stays hilited
  116.             if (oldFrontMostWindow)
  117.                 HiliteAndActivateWindow(oldFrontMostWindow,true);
  118.             }
  119.         else if (typeOfWindowToCreate == kNormalWindow)
  120.             {
  121.             ((WindowPeek) fWindow)->windowKind = kNormalWindowKind;
  122.  
  123.             //    unhighlight the old front window
  124.             if (oldFrontMostWindow)
  125.                 HiliteAndActivateWindow(oldFrontMostWindow,false);
  126.  
  127.             //    hilite the new window…
  128.             HiliteAndActivateWindow(fWindow,true);
  129.             }
  130.         }
  131.     }
  132.  
  133. void
  134. TWindow::AdjustCursor(EventRecord * /* anEvent */)
  135.     {
  136.     }
  137.  
  138. void
  139. TWindow::Idle(EventRecord * /* anEvent */)
  140.     {
  141.     }
  142.     
  143. void
  144. TWindow::Activate(Boolean /* activating */)
  145.     {
  146.     }
  147.     
  148. void
  149. TWindow::Draw(void)
  150.     {
  151.     }
  152.     
  153. void
  154. TWindow::Click(EventRecord * /* anEvent */)
  155.     {
  156.     }
  157.     
  158. void
  159. TWindow::KeyDown(EventRecord * /* anEvent */)
  160.     {
  161.     }
  162.  
  163. void
  164. TWindow::Select(void)
  165.     {
  166.     WindowPtr    currentFrontWindow;
  167.     
  168.     if (fWindowType == kFloatingWindow)
  169.         currentFrontWindow = FrontWindow();
  170.     else if (fWindowType == kNormalWindow)
  171.         currentFrontWindow = Front_NonFloatingWindow();
  172.     else
  173.         {
  174.         }
  175.  
  176.     if (currentFrontWindow != fWindow)
  177.         {
  178.         if (fWindowType == kFloatingWindow)
  179.             BringToFront(fWindow);
  180.         else
  181.             {
  182.             WindowPtr    lastFloater = LastFloatingWindow();
  183.  
  184.             //    If there are no floating windows,
  185.             //    just call SelectWindow like the good ol’ days
  186.  
  187.             if (lastFloater == kNoFloatingWindows)
  188.                 SelectWindow(fWindow);
  189.             else
  190.                 {
  191.                 // Deactivate the window currently in front.
  192.  
  193.                 HiliteAndActivateWindow(currentFrontWindow,false);
  194.     
  195.                 // Bring it behind the last floating window and activate it.
  196.                 // Note that Inside Mac 1 states that you need to call PaintOne() and CalcVis() on a
  197.                 // window if you are using SendBehind() to bring it closer to the front.  With System 7,
  198.                 // this is no longer necessary.
  199.  
  200.                 SendBehind(fWindow,lastFloater);
  201.                 HiliteAndActivateWindow(fWindow,true);
  202.                 }
  203.             }
  204.         }
  205.     }
  206.  
  207. void
  208. TWindow::Drag(Point startPoint)
  209.     {
  210.     GrafPtr        savePort;
  211.     KeyMap        theKeyMap;
  212.     Boolean        commandKeyDown = false;
  213.     RgnHandle    draggingRegion;
  214.     long        dragResult;
  215.     WindowPeek    windowAsWindowPeek = (WindowPeek) fWindow;
  216.     Boolean     gLiveDrag;
  217.     
  218.     
  219.     if (WaitMouseUp())        //    de-bounce?
  220.         {
  221.         // Set up the Window Manager port.
  222.     
  223.         GetPort(&savePort);
  224.         SetPort(gWindowManagerPort);
  225.         SetClip(GetGrayRgn());
  226.  
  227.         // Check to see if the command key is down.
  228.     
  229.         GetKeys(theKeyMap);
  230.         commandKeyDown = ((theKeyMap[1] & 0x8000) != 0);
  231.         gLiveDrag = (theKeyMap[1] >> 2) & 0x01 ;
  232.         
  233.         if (commandKeyDown)
  234.             {
  235.             //    We’re not going to change window ordering,
  236.             //    so make sure that we don’t drag in front of
  237.             //    other windows which may be in front of ours.
  238.  
  239. //    11/16/94    <Windows.h> on ETO defines this routine to take
  240. //                a WindowPtr instead of a WindowPeek. When building
  241. //                with new headers (like those included with ETO 16)
  242. //                make sure to define USEOLDUNIVERSALHEADERS to 0.
  243.  
  244. #if    USEOLDUNIVERSALHEADERS
  245.             ClipAbove(windowAsWindowPeek);
  246. #else
  247.             ClipAbove((WindowPtr) windowAsWindowPeek);
  248. #endif
  249.             }
  250.         else if (fWindowType != kFloatingWindow)
  251.             {
  252.             //    We’re dragging a normal window, so make sure
  253.             //    that we don’t drag in front of any floating
  254.             //    windows.
  255.  
  256. //    11/16/94    <Windows.h> on ETO defines this routine to take
  257. //                a WindowPtr instead of a WindowPeek. When building
  258. //                with new headers (like those included with ETO 16)
  259. //                make sure to define USEOLDUNIVERSALHEADERS to 0.
  260.  
  261. #if    USEOLDUNIVERSALHEADERS
  262.             ClipAbove((WindowPeek) Front_NonFloatingWindow());
  263. #else
  264.             ClipAbove(Front_NonFloatingWindow());
  265. #endif
  266.             }
  267.         
  268.         //    Drag an outline of the window around the desktop.
  269.         //    NOTE: DragGrayRgn destroys the region passed in, so make a copy
  270.  
  271.         //if (gLiveDrag) {
  272.             /*Rect r = {0,0,0,0};
  273.             Point origin = {0,0};    
  274.             Point diff;
  275.             EventRecord event;
  276.             GrafPtr tempPort;
  277.             
  278.             
  279.             GetPort(&tempPort);
  280.             SetPort(fWindow);    
  281.             (void)OSEventAvail(everyEvent,&event);
  282.             
  283.             LocalToGlobal(&origin);
  284.             diff.h = ( origin.h - event.where.h);
  285.             diff.v = ( origin.v -event.where.v);
  286.             //origin.h = windowAsWindowPeek->strucRgn[0]->rgnBBox.left;
  287.             //origin.v = windowAsWindowPeek->strucRgn[0]->rgnBBox.top;
  288.             SetPort(tempPort);
  289.             while (StillDown()) {
  290.                 EventRecord event;
  291.                 //short slop;
  292.                 
  293.                 //(void)OSEventAvail(everyEvent,&event);
  294.                 
  295.                 //ValidRect(&goodRect_Rect);
  296.                 (void)WaitNextEvent(everyEvent,&event,1,nil);
  297.                 
  298.                 if (event.what == updateEvt) {
  299.                     WindowRef window = (WindowRef)event.message;
  300.                     
  301.                     
  302.                     if (window) {
  303.                         TWindow * wobj;
  304.                         
  305.                         SetPort(window);
  306.                         BeginUpdate(window);
  307.                         wobj = GetWindowObject(window);
  308.  
  309.                         wobj->Draw();
  310.                         EndUpdate(window);
  311.                         SetPort(tempPort);
  312.                     }
  313.                     
  314.                     
  315.                 }
  316.                 
  317.                 event.where.h += diff.h ;
  318.                 event.where.v += diff.v;
  319.                 
  320.  
  321.                 
  322.                 
  323.                 if (fUseSMP)
  324.                 MoveWindow(fWindow,event.where.h,event.where.v,true);
  325.  
  326.                 Idle(nil);
  327.                 
  328.                 dragResult = 0;
  329.                 
  330.             }
  331.             */
  332.         //} else {
  333.             draggingRegion = NewRgn();
  334.             CopyRgn(windowAsWindowPeek->strucRgn,draggingRegion);
  335.             dragResult = DragGrayRgn(draggingRegion, startPoint, &gDeskRectangle, &gDeskRectangle, noConstraint, nil);
  336.             DisposeRgn(draggingRegion);
  337.  
  338.         //}
  339.         SetPort(savePort);    //    Get back to old port
  340.  
  341.         if ((dragResult != 0) && (dragResult != 0x80008000))
  342.             {
  343.             this->Nudge((short) (dragResult & 0xFFFF),(short) (dragResult >> 16));
  344.             }
  345.         }
  346.  
  347.     if (!commandKeyDown)
  348.         Select();
  349.     }
  350.  
  351. void
  352. TWindow::Nudge(short horizontalDistance, short verticalDistance)
  353.     {
  354.     WindowPeek    windowAsWindowPeek = (WindowPeek) fWindow;
  355.     short        newHorizontalPosition,newVerticalPosition;
  356.     
  357.     newHorizontalPosition = (short) (**windowAsWindowPeek->contRgn).rgnBBox.left + horizontalDistance;
  358.     newVerticalPosition = (short) (**windowAsWindowPeek->contRgn).rgnBBox.top + verticalDistance;
  359.  
  360.     MoveWindow(fWindow,newHorizontalPosition,newVerticalPosition,false);
  361.     }
  362.  
  363. void
  364. TWindow::Grow(Point startPoint)
  365.     {
  366.     GrafPtr    oldPort;
  367.     long    newSize;
  368.     Rect    oldWindowRect,resizeLimits;
  369.     
  370.     GetPort(&oldPort);
  371.     
  372.     GetWindowSizeLimits(&resizeLimits);
  373.     newSize = GrowWindow(fWindow,startPoint,&resizeLimits);
  374.     if (newSize)
  375.         {
  376.         oldWindowRect = fWindow->portRect;
  377.         SizeWindow(fWindow,(short) newSize,(short) (newSize >> 16),true);
  378.         SetPort(fWindow);
  379.         this->AdjustForNewWindowSize(&oldWindowRect,&fWindow->portRect);
  380.         }
  381.     
  382.     SetPort(oldPort);
  383.     }
  384.  
  385. void
  386. TWindow::Zoom(short zoomState)
  387.     {
  388.     GrafPtr        oldPort;
  389.     FontInfo    systemFontInfo;
  390.     short        titleBarHeight;
  391.     Rect        bestScreenRect,perfectWindowRect,scratchRect;
  392.     short        amountOffscreen;
  393.     WindowPeek    windowAsWindowPeek = (WindowPeek) fWindow;
  394.     GDHandle    bestDevice;
  395.     
  396.     GetPort(&oldPort);
  397.  
  398.     //    Figure out the height of the title bar so we can properly position
  399.     //    a window. The algorithm is stolen from the System 7.x 'WDEF' (0)
  400.     //
  401.     //    This probably isn’t the best thing to do: A better way might be 
  402.     //    to diff the structure and content region rectangles?
  403.  
  404.     SetPort(gWindowManagerPort);
  405.     GetFontInfo(&systemFontInfo);
  406.     titleBarHeight = (short) (systemFontInfo.ascent + systemFontInfo.descent + 4);
  407.     if ((titleBarHeight % 2) == 1)
  408.         titleBarHeight--;
  409.     if (titleBarHeight < kMinimumTitleBarHeight)
  410.         titleBarHeight = kMinimumTitleBarHeight;
  411.  
  412.     //    Only do the voodoo magic if we are really “zooming” the window.
  413.  
  414.     if (zoomState == inZoomOut)
  415.         {
  416.         FindScreenRectWithLargestPartOfWindow(fWindow,&bestScreenRect,&bestDevice);
  417.         bestScreenRect.top += titleBarHeight;
  418.  
  419.         this->GetPerfectWindowSize(&perfectWindowRect);
  420.         OffsetRect(&perfectWindowRect,-perfectWindowRect.left,-perfectWindowRect.top);
  421.  
  422.         //    Take the zero-pined perfect window size and move it to
  423.         //    the top left of the    window’s content region.
  424.  
  425.         OffsetRect(&perfectWindowRect,(**windowAsWindowPeek->contRgn).rgnBBox.left,
  426.                                       (**windowAsWindowPeek->contRgn).rgnBBox.top);
  427.  
  428.         
  429.         //    Does perfectWindowRect fit completely on the best screen?
  430.         
  431.         SectRect(&perfectWindowRect, &bestScreenRect, &scratchRect);
  432.         if (!EqualRect(&perfectWindowRect, &scratchRect))
  433.             {
  434.             //    SectRect sez perfectWindowRect doesn’t completely fit
  435.             //    on the screen, so bump the window so that more of it fits.
  436.  
  437.             //    Make sure that the left edge of perfectWindowRect is forced
  438.             //    onto the best screen.  This is in case we are bumping
  439.             //    the window to the right.
  440.  
  441.             amountOffscreen = bestScreenRect.left - perfectWindowRect.left;
  442.             if (amountOffscreen > 0)
  443.                 {
  444.                 perfectWindowRect.left += amountOffscreen;
  445.                 perfectWindowRect.right += amountOffscreen;
  446.                 }
  447.  
  448.             //    Make sure that the left edge of perfectWindowRect is forced
  449.             //    onto the best screen.  This is in case we are bumping
  450.             //    the window downward to a new screen.
  451.     
  452.             amountOffscreen = bestScreenRect.top - perfectWindowRect.top;
  453.             if (amountOffscreen > 0)
  454.                 {
  455.                 perfectWindowRect.top += amountOffscreen;
  456.                 perfectWindowRect.bottom += amountOffscreen;
  457.                 }
  458.  
  459.             //    If right edge of window falls off the screen,
  460.             //        Move window to the left until the right edge IS on the screen
  461.             //        OR the left edge is at bestScreenRect.left
  462.  
  463.             amountOffscreen = perfectWindowRect.right - bestScreenRect.right;
  464.             if (amountOffscreen > 0)
  465.                 {
  466.                 //    Are we going to push the left edge offscreen? If so, change the
  467.                 //    offset so we move the window all the way over to the left.
  468.                 
  469.                 if ((perfectWindowRect.left - amountOffscreen) < bestScreenRect.left)
  470.                     amountOffscreen = perfectWindowRect.left - bestScreenRect.left;
  471.  
  472.                 perfectWindowRect.left -= amountOffscreen;
  473.                 perfectWindowRect.right -= amountOffscreen;
  474.                 }
  475.  
  476.             //    If bottom edge of window falls off the screen,
  477.             //        Move window to up until the bottom edge IS on the screen
  478.             //        OR the top edge is at bestScreenRect.top
  479.  
  480.             amountOffscreen = perfectWindowRect.bottom - bestScreenRect.bottom;
  481.             if (amountOffscreen > 0)
  482.                 {
  483.                 //    Are we going to push the top edge offscreen? If so, change the
  484.                 //    offset so we move the window just to the top.
  485.                 
  486.                 if ((perfectWindowRect.top - amountOffscreen) < bestScreenRect.top)
  487.                     amountOffscreen = perfectWindowRect.top - bestScreenRect.top;
  488.  
  489.                 perfectWindowRect.top -= amountOffscreen;
  490.                 perfectWindowRect.bottom -= amountOffscreen;
  491.                 }
  492.  
  493.             SectRect(&perfectWindowRect, &bestScreenRect, &scratchRect);
  494.             if (!EqualRect(&perfectWindowRect, &scratchRect))
  495.                 {
  496.                 //    The edges of the window still fall offscreen,
  497.                 //    so make the window smaller until it fits.
  498.                 
  499.                 if (perfectWindowRect.bottom > bestScreenRect.bottom)
  500.                     perfectWindowRect.bottom = bestScreenRect.bottom;
  501.  
  502.                 //    If the right edge is still falling off,
  503.                 //        save space for Finder’s disk icons as well.
  504.  
  505.                 if (perfectWindowRect.right > bestScreenRect.right)
  506.                     {
  507.                     perfectWindowRect.right = bestScreenRect.right;
  508.                     
  509.                     //    If we were on the main screen, leave room for Finder icons, too.
  510.                     
  511.                     if (bestDevice == GetMainDevice())
  512.                         perfectWindowRect.right -= kSpaceForFinderIcons;
  513.                     }
  514.                 }
  515.             }
  516.  
  517.         //    Stash our new rectangle inside of the Window’s dataHandle
  518.         //    so that ZoomWindow does the right thing.
  519.         
  520.         (**((WStateDataHandle) (windowAsWindowPeek->dataHandle))).stdState = perfectWindowRect;
  521.         }
  522.  
  523.     //    HEY YOU! Don’t forget to set the port to the window being zoomed
  524.     //    Why, you ask? Because IM-IV-50 says to; otherwise you die
  525.     
  526.     SetPort(fWindow);
  527.  
  528.     Rect    oldWindowRect = fWindow->portRect;
  529.     
  530.     ZoomWindow(fWindow,zoomState,false);
  531.     this->AdjustForNewWindowSize(&oldWindowRect,&fWindow->portRect);
  532.  
  533.     SetPort(oldPort);
  534.     }
  535.  
  536. void
  537. TWindow::ShowHide(Boolean showFlag)
  538.     {
  539.     //    Here we need the “::” in front of ShowHide to indicate we are calling
  540.     //    the global function, and not the method ShowHide. Unintended recursion
  541.     //    can do bad things to the unsuspecting programmer.
  542.     
  543.     //    Some C++ programmers would always prepend the “::” on function calls.
  544.     
  545.     ::ShowHide(fWindow,showFlag);
  546.     fIsVisible = showFlag;
  547.     }
  548.     
  549.  
  550. Boolean
  551. TWindow::EventFilter(EventRecord * /* theEvent */)
  552.     {
  553.     return false;
  554.     }
  555.     
  556.  
  557. void
  558. TWindow::GetPerfectWindowSize(Rect *perfectSize)
  559.     {
  560.     *perfectSize = qd.screenBits.bounds;
  561.     }
  562.  
  563. void
  564. TWindow::GetWindowSizeLimits(Rect *limits)
  565.     {
  566.     limits->top = limits->left = kMinimumWindowSize;
  567.     limits->right = gDeskRectangle.right - gDeskRectangle.left;
  568.     limits->bottom = gDeskRectangle.bottom - gDeskRectangle.top;
  569.     }
  570.  
  571. void
  572. TWindow::AdjustForNewWindowSize(Rect * /* oldRect */, Rect * /* newSize */)
  573.     {
  574.     }
  575.  
  576. Boolean
  577. TWindow::IsVisible(void)
  578.     {
  579.     return fIsVisible;
  580.     }
  581.  
  582. Boolean
  583. TWindow::CanClose(void)
  584.     {
  585.     return true;
  586.     }
  587.  
  588. Boolean
  589. TWindow::Close(void)
  590.     {
  591.     WindowPtr    newFrontWindow = nil;
  592.     
  593.     if (Front_NonFloatingWindow() == fWindow)
  594.         newFrontWindow = (WindowPtr) ((WindowPeek) fWindow)->nextWindow;
  595.  
  596.     this->Activate(false);
  597.     DisposeWindow(fWindow);
  598.  
  599.     if (newFrontWindow)
  600.         HiliteAndActivateWindow(newFrontWindow,true);
  601.  
  602.     return true;
  603.     }
  604.  
  605. Boolean
  606. TWindow::DeleteAfterClose(void)
  607.     {
  608.     return true;
  609.     }
  610.  
  611. void
  612. TWindow::AdjustMenusBeforeMenuSelection(void)
  613.     {
  614.     }
  615.  
  616.     
  617. void
  618. TWindow::DoMenuSelection(short /* menu */, short /* item */)
  619.     {
  620.     }
  621.     
  622.  
  623. void
  624. TWindow::DoMenuCommand(unsigned long /* menuCommand */)
  625.     {
  626.     }
  627.  
  628. OSErr
  629. TWindow::HandleDrag(DragTrackingMessage dragMessage,DragReference theDrag)
  630.     {
  631.     OSErr    result = dragNotAcceptedErr;
  632.     
  633.     switch (dragMessage)
  634.         {
  635.         case    kDragTrackingEnterWindow:
  636.             result = this->DragEnterWindow(theDrag);
  637.             break;
  638.         
  639.         case    kDragTrackingInWindow:
  640.             result = this->DragInWindow(theDrag);
  641.             break;
  642.             
  643.         case    kDragTrackingLeaveWindow:
  644.             result = this->DragLeaveWindow(theDrag);
  645.             break;
  646.             
  647.         default:
  648.             break;
  649.         }
  650.  
  651.     return result;
  652.     }
  653.  
  654. OSErr
  655. TWindow::DragEnterWindow(DragReference /* theDrag */)
  656.     {
  657.     return dragNotAcceptedErr;
  658.     }
  659.  
  660. OSErr
  661. TWindow::DragInWindow(DragReference /* theDrag */)
  662.     {
  663.     return dragNotAcceptedErr;
  664.     }
  665.  
  666. OSErr
  667. TWindow::DragLeaveWindow(DragReference /* theDrag */)
  668.     {
  669.     return dragNotAcceptedErr;
  670.     }
  671.     
  672.  
  673. OSErr
  674. TWindow::HandleDrop(DragReference /* theDrag */)
  675.     {
  676.     return dragNotAcceptedErr;
  677.     }
  678.  
  679. ///////////////////////////////////////////////////////////////////////////
  680. //
  681. //    Utility Functions used for floating windows
  682. //
  683.  
  684. TWindow *
  685. GetWindowObject(WindowPtr aWindow)
  686.     {
  687.     short    wKind;
  688.     
  689.     if (aWindow != nil)
  690.         {
  691.         wKind = ((WindowPeek) aWindow)->windowKind;
  692.  
  693.         if (wKind >= userKind)
  694.             {
  695.             //    All windowKinds >= userKind are based upon TWindow
  696.  
  697.             return (TWindow *) GetWRefCon(aWindow);
  698.             }
  699.         }
  700.     return (TWindow *) nil;
  701.     }
  702.  
  703. ////////////////////////////////////////////////////////////////////////
  704. //
  705. //    Utility functions
  706.  
  707. pascal WindowPtr
  708. GetNewColorOrBlackAndWhiteWindow(short windowID, void *wStorage, WindowPtr behind)
  709.     {
  710.     if (gHasColorQuickdraw)
  711.         return GetNewCWindow(windowID,wStorage,behind);
  712.     else
  713.         return GetNewWindow(windowID,wStorage,behind);
  714.     }
  715.  
  716. pascal WindowPtr
  717. NewColorOrBlackAndWhiteWindow(void *wStorage, const Rect *boundsRect, ConstStr255Param title, Boolean visible, short theProc, WindowPtr behind, Boolean goAwayFlag, long refCon)
  718.     {
  719.     if (gHasColorQuickdraw)
  720.         return NewCWindow(wStorage,boundsRect,title,visible,theProc,behind,goAwayFlag,refCon);
  721.     else
  722.         return NewWindow(wStorage,boundsRect,title,visible,theProc,behind,goAwayFlag,refCon);
  723.     }
  724.  
  725. void
  726. DrawJustTheGrowIcon(WindowPtr aWindow)
  727.     {
  728.     GrafPtr        savedPort;
  729.     RgnHandle    savedClip = NewRgn();
  730.     Rect        growBoxRect;
  731.     
  732.     GetPort(&savedPort);
  733.     SetPort(aWindow);
  734.     GetClip(savedClip);
  735.  
  736.     //    clip to just the bottom right corner of the window
  737.     
  738.     growBoxRect.top = aWindow->portRect.bottom - kScrollbarWidth;
  739.     growBoxRect.bottom = aWindow->portRect.bottom;
  740.     growBoxRect.left = aWindow->portRect.right - kScrollbarWidth;
  741.     growBoxRect.right = aWindow->portRect.right;
  742.     ClipRect(&growBoxRect);
  743.  
  744.     DrawGrowIcon(aWindow);
  745.  
  746.     SetClip(savedClip);
  747.     DisposeRgn(savedClip);    
  748.  
  749.     SetPort(savedPort);
  750.     }
  751.     
  752.  
  753. WindowPtr
  754. LastFloatingWindow(void)
  755.     {
  756.     WindowPeek    aWindow = (WindowPeek) FrontWindow();
  757.     WindowPtr    lastFloater = (WindowPtr) kNoFloatingWindows;
  758.     
  759.     while (aWindow && (aWindow->windowKind == kFloatingWindowKind))
  760.         {
  761.         if (aWindow->visible)
  762.             lastFloater = (WindowPtr) aWindow;
  763.  
  764.         aWindow = (WindowPeek) aWindow->nextWindow;
  765.         }
  766.     return(lastFloater);
  767.     }
  768.  
  769. WindowPtr
  770. Front_NonFloatingWindow(void)
  771.     {
  772.     WindowPeek    aWindow = (WindowPeek) LMGetWindowList();
  773.  
  774.     //    Skip over floating windows
  775.         
  776.     while (aWindow && (aWindow->windowKind == kFloatingWindowKind))
  777.         aWindow = (WindowPeek) aWindow->nextWindow;
  778.  
  779.     //    Skip over invisible, but otherwise normal windows
  780.     
  781.     while (aWindow && (aWindow->visible == 0))
  782.         aWindow = (WindowPeek) aWindow->nextWindow;
  783.         
  784.     return (WindowPtr) aWindow;
  785.     }
  786.  
  787. void
  788. HiliteAndActivateWindow(WindowPtr aWindow,Boolean active)
  789.     {
  790.     GrafPtr        oldPort;
  791.     TWindow    *    wobj = GetWindowObject(aWindow);
  792.     
  793.     if (aWindow)
  794.         {
  795.         HiliteWindow(aWindow,active);
  796.  
  797.         if (wobj != nil)
  798.             {
  799.             GetPort(&oldPort);
  800.             SetPort(aWindow);
  801.             wobj->Activate(active);
  802.             SetPort(oldPort);
  803.             }    
  804.         }
  805.     }
  806.  
  807. void
  808. SuspendResumeWindows(Boolean resuming)
  809.     {
  810.     //    When we suspend/resume, hide/show all the visible floaters
  811.     
  812.     HiliteShowHideFloatingWindows(resuming,true);
  813.     }
  814.  
  815. void
  816. HiliteWindowsForModalDialog(Boolean hiliting)
  817.     {
  818.     //    When we display a modal dialog, we need to unhighlight
  819.     //    all visible floaters. We also need to re-hilite them
  820.     //    afterwards.
  821.     
  822.     HiliteShowHideFloatingWindows(hiliting,false);
  823.     }
  824.  
  825. void
  826. HiliteShowHideFloatingWindows(Boolean hiliting,Boolean dohiding)
  827.     {
  828.     WindowPeek    aWindow;
  829.     TWindow *    wobj;
  830.     
  831.     HiliteAndActivateWindow(Front_NonFloatingWindow(),hiliting);
  832.  
  833.     aWindow = (WindowPeek) LMGetWindowList();
  834.     while (aWindow && aWindow->windowKind == kFloatingWindowKind)
  835.         {
  836.         wobj = GetWindowObject((WindowPtr) aWindow);
  837.         
  838.         //    If we are hiding or showing, only hide/show windows
  839.         //    that were visible to begin with.
  840.         
  841.         //    NOTE:    We use our copy of the visible flag so we can
  842.         //            automatically show floaters on a resume event.
  843.         
  844.         //    NOTE:    Since this isn’t a method of TWindow, we don’t
  845.         //            really need the “::” on ShowHide, but as long
  846.         //            as we’re trying to avoid ambiguity.
  847.         
  848.         if (dohiding && (wobj != nil) && (wobj->IsVisible()))
  849.             ::ShowHide((WindowPtr) aWindow,hiliting);
  850.             
  851.         //    All floaters are hilited if any floater is hilited
  852.  
  853.         HiliteWindow((WindowPtr) aWindow,hiliting);
  854.         aWindow = (WindowPeek) aWindow->nextWindow;
  855.         }
  856.     }
  857.  
  858. ///////////////////////////////////////////////////////////////////////////
  859. //
  860. //    Routines used for dealing with windows and multiple screens
  861. //
  862.  
  863. pascal void
  864. CalculateWindowAreaOnDevice(short /* depth */,short /* deviceFlags */,GDHandle targetDevice,long userData)
  865.     {
  866.     CalcWindowAreaDeviceLoopUserData *    deviceLoopDataPtr;
  867.     long                                windowAreaOnThisScreen;
  868.     Rect                                windowRectOnThisScreen;
  869.     
  870.     deviceLoopDataPtr = (CalcWindowAreaDeviceLoopUserData *) userData;
  871.  
  872.     SectRect(&deviceLoopDataPtr->fWindowBounds, &(**targetDevice).gdRect,&windowRectOnThisScreen);
  873.     OffsetRect(&windowRectOnThisScreen,-windowRectOnThisScreen.left,-windowRectOnThisScreen.top);
  874.     windowAreaOnThisScreen = windowRectOnThisScreen.right * windowRectOnThisScreen.bottom;
  875.  
  876.     if (windowAreaOnThisScreen > deviceLoopDataPtr->fLargestArea)
  877.         {
  878.         deviceLoopDataPtr->fLargestArea = windowAreaOnThisScreen;
  879.         deviceLoopDataPtr->fScreenWithLargestPartOfWindow = targetDevice;
  880.         }
  881.     }
  882.  
  883. DeviceLoopDrawingUPP CallCalcWindowAreaOnDevice = NewDeviceLoopDrawingProc(&CalculateWindowAreaOnDevice);
  884.  
  885. void
  886. FindScreenRectWithLargestPartOfWindow(WindowPtr aWindow,Rect *theBestScreenRect,GDHandle * theBestDevice)
  887.     {
  888.     RgnHandle                            copyOfWindowStrucRgn;
  889.     CalcWindowAreaDeviceLoopUserData    deviceLoopData;
  890.  
  891.     //    Use DeviceLoop to find out what GDevice contains the largest
  892.     //    portion of the supplied window.
  893.     //
  894.     //    NOTE:    Assumes thePort == the Window Manager Port because we using
  895.     //            the window strucRgn, not contRgn.
  896.  
  897.     deviceLoopData.fScreenWithLargestPartOfWindow = nil;
  898.     deviceLoopData.fLargestArea = -1;
  899.     deviceLoopData.fWindowBounds = (**(((WindowPeek) aWindow)->contRgn)).rgnBBox;
  900.     
  901.     copyOfWindowStrucRgn = NewRgn();
  902.     CopyRgn(((WindowPeek) aWindow)->strucRgn,copyOfWindowStrucRgn);
  903.  
  904.     DeviceLoop(copyOfWindowStrucRgn,CallCalcWindowAreaOnDevice,(long) &deviceLoopData,singleDevices);    
  905.  
  906.     DisposeRgn(copyOfWindowStrucRgn);
  907.     
  908.     *theBestDevice = deviceLoopData.fScreenWithLargestPartOfWindow;
  909.     *theBestScreenRect = (**(deviceLoopData.fScreenWithLargestPartOfWindow)).gdRect;
  910.  
  911.     //    Leave some space around the edges of the screen so window look good, AND
  912.     //    if the best device is the main screen, leave space for the Menubar
  913.     
  914.     InsetRect(theBestScreenRect,kScreenEdgeSlop,kScreenEdgeSlop);
  915.     if (GetMainDevice() == deviceLoopData.fScreenWithLargestPartOfWindow)
  916.         theBestScreenRect->top += GetMBarHeight();
  917.     }
  918.  
  919. ///////////////////////////////////////////////////////////////////////////
  920. //
  921. //    Drag Manager callback routines which dispatch to a window’s method
  922. //
  923.  
  924. pascal OSErr
  925. CallWindowDragTrackingHandler(DragTrackingMessage dragMessage,WindowPtr theWindow,void * /* refCon */,DragReference theDrag)
  926.     {
  927.     TWindow *wobj = GetWindowObject(theWindow);
  928.     
  929.     if (wobj)
  930.         return(wobj->HandleDrag(dragMessage,theDrag));
  931.     else
  932.         return dragNotAcceptedErr;
  933.     }
  934.  
  935.     
  936. pascal OSErr
  937. CallWindowDragReceiveHandler(WindowPtr theWindow,void * /* refCon */,DragReference theDrag)
  938.     {
  939.     TWindow *wobj = GetWindowObject(theWindow);
  940.     
  941.     if (wobj)
  942.         return(wobj->HandleDrop(theDrag));
  943.     else
  944.         return dragNotAcceptedErr;
  945.     }
  946.